package trans;

import db.DatabaseConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


// 收入记账
public class IncomeTransaction implements Transaction {
    private String description;
    private double amount;
    private int id;

    public IncomeTransaction(String description, double amount,int id) {
        this.description = description;
        this.amount = amount;
        this.id = id;
    }

    @Override
    public void execute() {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = DatabaseConnection.getInstance().getConnection();
            stmt = conn.prepareStatement(
                    "INSERT INTO transactions (id, description, amount, type) VALUES (?, ?, ?, ?)");
            stmt.setInt(1, id);
            stmt.setString(2, description);
            stmt.setDouble(3, amount);
            stmt.setString(4, "收入");
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    public double getAmount() {
        return amount;
    }

    @Override
    public String toString() {
        return "id:"+id +",收入: " + description + ", 金额: " + amount;
    }
}